import { Environment } from '@/utils'
import PinyinMatch from 'pinyin-match'

export function isMatch<T extends { account?: string; name?: string }>(mcc: T, keyword: string) {
	if (keyword === '') return true
	const match1 = PinyinMatch.match(mcc.account || '', keyword)
	const match2 = PinyinMatch.match(mcc.name || '', keyword)
	if (match1 === false && match2 === false) {
		return false
	} else {
		return true
	}
}
export function compareMtKey(mtKey1: string, mtKey2: string) {
	const a = mtKey1.match(/((\S+(\.|_)))?(\S+)/)
	const b = mtKey2.match(/((\S+(\.|_)))?(\S+)/)
	if (a && b && a[4] && b[4]) {
		return parseInt(a[4]) - parseInt(b[4])
	} else {
		return 0
	}
}
export function compareVersion(v1: string, v2: string) {
	if (v1 === v2) {
		return false
	}
	let arr1 = v1.split(',')
	let arr2 = v2.split(',')
	const len = Math.max(arr1.length, arr2.length)
	// 调整两个版本号位数相同
	while (arr1.length < len) {
		arr1.push('0')
	}
	while (arr2.length < len) {
		arr2.push('0')
	}
	// 循环判断每位数的大小
	for (let i = 0; i < len; i++) {
		const num1 = parseInt(arr1[i])
		const num2 = parseInt(arr2[i])
		if (num1 > num2) {
			return true
		} else if (num1 < num2) {
			return false
		}
	}
	return true
}
export function hasBigVersion(newVersion: string, originVersion: string) {
	//大于
	var newVersionArr = newVersion.split(',')
	var originVersionArr = originVersion.split(',')
	for (var i = 0; i < newVersion.length; i++) {
		if (i == 3) continue //不判断小版本
		if (parseInt(newVersionArr[i]) > parseInt(originVersionArr[i])) {
			return true
		}
		if (parseInt(newVersionArr[i]) < parseInt(originVersionArr[i])) {
			return false
		}
	}
	return false
}
export function isLinuxSys() {
	//是否是ilinux系统
	let item = localStorage.getItem('isLinuxSys')
	if (item !== null && item.length > 0) {
		return item == '1' ? true : false
	}
	var agent = navigator.userAgent
	let isLinux = String(navigator.platform).indexOf('Linux') > -1
	if (agent.indexOf('Linux') > -1 || isLinux) {
		return true
	}
	return false
}
//获取浏览器左下角位置
export function getBrowserLeftBottomPosition() {
	if (!window.screenLeft) {
		window.screenLeft = window.screenX
		window.screenTop = window.screenY
	}
	//窗口左下角
	var pos = {
		x: window.screenLeft + 8,
		y: window.screenTop + window.outerHeight - 8,
		full_x: false,
		full_y: false,
	}
	if (Environment.isIE) {
		pos.x = pos.x - 8
		pos.y = window.screenTop + window.innerHeight
	}
	//如果最大则去掉修正
	if (window.screen.availWidth <= window.outerWidth) {
		pos.x = 0
		pos.full_x = true
	}
	if (window.screen.availHeight <= window.outerHeight) {
		pos.y = window.outerHeight
		pos.full_y = true
	}
	return pos
}
//获取dom节点的相对屏幕位
export function getDomScreenPosition(element: Element) {
	if (!window.screenLeft) {
		window.screenLeft = window.screenX
		window.screenTop = window.screenY
	}
	var viewTop = window.screenTop + (window.outerHeight - window.innerHeight - 8)
	var viewLeft = window.screenLeft + (window.screen.width == window.outerWidth ? 0 : 8)
	if (Environment.isIE) {
		viewTop = window.screenTop
		viewLeft = window.screenLeft
	}
	let offset = getOffset(element)
	//floor 兼容缩放 保证位置相对正确
	var pos = {
		y: viewTop + Math.floor(offset.top),
		x: viewLeft + Math.floor(offset.left),
	}
	return pos
}
export const getOffset = (element: Element) => {
	const elemRect = element.getBoundingClientRect()
	const offsetY = elemRect.top + window.scrollY
	const offsetX = elemRect.left + window.scrollX
	return {
		left: offsetX,
		top: offsetY,
	}
}
